package com.wonders.cyxtjsonupdate;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.wonders.cyxtjsonupdate.entity.Cyxt;
import com.wonders.cyxtjsonupdate.service.CyxtJsonData;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.*;

@SpringBootTest
class CyxtJsonUpdateApplicationTests2 {

    //-----------大屏记录--首页页面更新

    @Autowired
    CyxtJsonData cyxtJsonData;

    @Test
    void test1() {
        Calendar cal = Calendar.getInstance();
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMaximumFractionDigits(0);//保留0位
        //获得当前月份
        int month = new Date().getMonth();
        month = 11;//因目前1月份数据库未产生12月份数据，此此先模拟
        //获得当前年份
        int year = cal.get(Calendar.YEAR);
        ArrayList<String> list = new ArrayList<>();//用来存储所有的最终的json
        //初始化全社会货物运输量
        List<Map<String, Object>> societyList = cyxtJsonData.init();
        //初始化全港货物吞吐量
        List<Map<String, Object>> harborList = cyxtJsonData.initHarborThroughput();
        //初始化集装箱吞吐量
        List<Map<String, Object>> containerList = cyxtJsonData.initcontainer();
        //初始化公路旅客发送量的list
        List<Map<String, Object>> highwayList = cyxtJsonData.initHighway();
        //初始化水路旅客发送量的list
        List<Map<String, Object>> waterWayList = cyxtJsonData.initWaterway();
        //初始化铁路旅客发送量的list
        List<Map<String, Object>> railWayList = cyxtJsonData.initrailway();
        //初始化航空旅客发送量的list
        List<Map<String, Object>> aviationList = cyxtJsonData.initaviation();

        //初始化公路货物运输量
        List<Map<String, Object>> roadTransportList = cyxtJsonData.initRoadTransport();
        //初始化水路货物运输量
        List<Map<String, Object>> waterWayFreightList = cyxtJsonData.initWaterWayTransport();
        //初始化铁路货物运输量
        List<Map<String, Object>> railwayList = cyxtJsonData.initrailwayTransport();
        //初始化航空货物运输量
        List<Map<String, Object>> airList = cyxtJsonData.initairTransport();






        //---日均全社会货物运输量（吨）
        Cyxt cyxt1 = buildgeneralpParame("dfa31eaa9393444f9fe6da6978657a3b", "indexLazy", "rjqshhwyxl");
        buildTransportationOfDay(cyxt1,year,month,societyList,list,nf);

        //----日均全港货物吞吐量（吨）
        Cyxt cyxt2 = buildgeneralpParame("b24a7f898c354689830ab56eee1b3bf8", "indexLazy", "rjqghwttl");
        buildTransportationOfDay(cyxt2,year,month,harborList,list,nf);

        //-----日均集装箱吞吐量（TEU）
        Cyxt cyxt3 = buildgeneralpParame("dd2c957fd0b84ce4b9119e88578ec4b7", "indexLazy", "rjjzxttl");
        buildTransportationOfDay(cyxt3,year,month,containerList,list,nf);

        //-----右方饼图(累计)
        Cyxt cyxt4 = buildgeneralpParame("dd2027828d5045eebad1eaa40a2975dc", "indexLazy", "chart1");
        buildRightChart(cyxt4,month,highwayList,waterWayList,railWayList,aviationList,list);

        //------港口货物吞吐量圈图
        Cyxt cyxt5 = buildgeneralpParame("b62550340c0a474983f4a61504f96b22", "indexLazy", "chart2");
        buildPortCargo(cyxt5,month,harborList,list);
        //------集装箱吞吐量圈图
        Cyxt cyxt6 = buildgeneralpParame("ed15abb530ee4fe3968302832159b0ec", "indexLazy", "chart3");
        buildPortCargo(cyxt6,month,containerList,list);
        //-----四种方式【货运】（折线图）
        Cyxt cyxt7 = buildgeneralpParame("1832a6e0b4ea4b62a266f9325910b5a7", "indexLazy", "chart4");
        buildFreightChart(cyxt7,month,roadTransportList,waterWayFreightList,railwayList,airList,list);
        //-------四种方式【客运】（折线图）
        Cyxt cyxt8 = buildgeneralpParame("1f56fc2ef9274e66b8042a7bc9ca588b", "indexLazy", "chart5");
        buildPassengerTransport(cyxt8,month,highwayList,waterWayList,railWayList,aviationList,list);

        System.out.println(list);



    }

    /**
     * Methad info:-----右方饼图(累计)
     * @author 陈兆庆
     * @date 2022/1/6 10:12
     */
    public void buildRightChart(Cyxt cyxt,int month, List<Map<String, Object>> highwayList,List<Map<String, Object>> waterWayList, List<Map<String, Object>> railWayList,List<Map<String, Object>> aviationList,List list){
      //公路旅客发送量
        Object yearsumgl = highwayList.get(month - 1).get("YEARSUM");
        int gl= (int)(Double.parseDouble(yearsumgl.toString())*10000);
      //水路旅客发送量
        Object yearsumsl = waterWayList.get(month - 1).get("YEARSUM");
        int sl= (int)(Double.parseDouble(yearsumsl.toString())*10000);
      //铁路旅客发送量
        Object yearsumtl = railWayList.get(month - 1).get("YEARSUM");
        int tl= (int)(Double.parseDouble(yearsumtl.toString())*10000);
      //航空旅客发送量
        Object yearsumhk = aviationList.get(month - 1).get("YEARSUM");
        int hk= (int)(Double.parseDouble(yearsumhk.toString())*10000);
        cyxt.setDataItem1("[{value: "+gl+", name: '公路旅客发送量(人次)'},{value: "+sl+", name: '水路旅客发送量(人次)'},{value: "+tl+" , name: '铁路旅客发送量(人次)'},{value: "+hk+", name: '航空旅客发送量(人次)'}]");
        String yjhy = JSONObject.toJSONString(cyxt, SerializerFeature.WriteMapNullValue);
        list.add(yjhy);
    }



   /**
    * Methad info:日均运输量
    * @author 陈兆庆
    * @date 2022/1/6 10:23
    */
    public void buildTransportationOfDay(Cyxt cyxt,int year,int month,List<Map<String, Object>> dataList,List list,NumberFormat nf){
        int days = getDays(year, month);
        Object yearsum = dataList.get(month-1).get("YEARSUM");
        double yearsumd = Double.parseDouble(yearsum.toString());
        double monthavg = yearsumd / days * 10000;
        String format = nf.format(monthavg);
        format=format.replace(""," ").trim();
        cyxt.setDataItem1(format);
        String yjhy = JSONObject.toJSONString(cyxt, SerializerFeature.WriteMapNullValue);
        list.add(yjhy);
    }

    /**
     * Methad info:------货物吞吐量圈图
     * @author 陈兆庆
     * @date 2022/1/6 11:09
     */
    public void buildPortCargo(Cyxt cyxt,int month,List<Map<String, Object>> dataList,List list){
        ArrayList<Integer>cargoList= new ArrayList<>();
        for (int i = 0; i < month; i++) {
            Object monthsym = dataList.get(i).get("MONTHSUM");
            int data= (int)(Double.parseDouble(monthsym.toString())*10000);
            cargoList.add(data);
        }
        cyxt.setDataItem1(cargoList.toString().replace(" ",""));
        cyxt.setDataItem2(getMonth2(month).toString().replace(" ",""));
        String yjhy = JSONObject.toJSONString(cyxt, SerializerFeature.WriteMapNullValue);
        list.add(yjhy);

    }

    /**
     * Methad info:------四种方式【货运】（折线图）
     * @author 陈兆庆
     * @date 2022/1/6 13:47
     */
    public void buildFreightChart(Cyxt cyxt,int month,List<Map<String, Object>> roadTransportList,List<Map<String, Object>> waterWayFreightList,List<Map<String, Object>> railwayList,List<Map<String, Object>> airList,List list){
        cyxt.setDataItem1(getMonth2(month).toString().replace(" ",""));
        //--公路折线图
        cyxt.setDataItem2(buildFreightData(month,roadTransportList).toString().replace(" ",""));
        //--水路折线图
        cyxt.setDataItem3(buildFreightData(month,waterWayFreightList).toString().replace(" ",""));
        //--铁路折线图
        cyxt.setDataItem4(buildFreightData(month,railwayList).toString().replace(" ",""));
        //--铁路折线图
        cyxt.setDataItem5(buildFreightData(month,airList).toString().replace(" ",""));
        String datastr = JSONObject.toJSONString(cyxt, SerializerFeature.WriteMapNullValue);
        list.add(datastr);
    }

    /**
     * Methad info:-------四种方式【客运】（折线图）
     * @author 陈兆庆
     * @date 2022/1/6 14:03
     */
     public void buildPassengerTransport(Cyxt cyxt,int month,List<Map<String, Object>> highwayList,List<Map<String, Object>> waterWayList,List<Map<String, Object>> railWayList,List<Map<String, Object>> aviationList,List list){
         cyxt.setDataItem1(getMonth2(month).toString().replace(" ",""));
         //--公路折线图
         cyxt.setDataItem2(buildFreightData(month,highwayList).toString().replace(" ",""));
         //--水路折线图
         cyxt.setDataItem3(buildFreightData(month,waterWayList).toString().replace(" ",""));
         //--铁路折线图
         cyxt.setDataItem4(buildFreightData(month,railWayList).toString().replace(" ",""));
         //--铁路折线图
         cyxt.setDataItem5(buildFreightData(month,aviationList).toString().replace(" ",""));
         String datastr = JSONObject.toJSONString(cyxt, SerializerFeature.WriteMapNullValue);
         list.add(datastr);

     }



    /**
     * Methad info:四种方式【货运】【客运】（折线图）通用方法
     * @author 陈兆庆
     * @date 2022/1/6 13:54
     */
   public List buildFreightData(int month,List<Map<String, Object>> dataList){
       ArrayList<Integer> freightList = new ArrayList<>();
       for (int i = 0; i < month; i++) {
           Object monthsym = dataList.get(i).get("MONTHSUM");
           int data= (int)(Double.parseDouble(monthsym.toString())*10000);
           freightList.add(data);
       }
       return freightList;
   }


    /**
     * Methad info:四个折线图(月趋势)
     * @author 陈兆庆
     * @date 2022/1/6 10:36
     */
    public void buildChart(String id, String mainType, String subType, int month, String keys, String snty, List monthList, List<Map<String, Object>> listdata, List list) {
        Cyxt cyxt = new Cyxt();
        cyxt.setId(id);
        cyxt.setMainType(mainType);
        cyxt.setSubType(subType);
        cyxt.setDataItem1(monthList.toString());
        ArrayList<Integer> roadChart1 = new ArrayList<>();
        for (int i = 0; i < month; i++) {
            Object monthsum = listdata.get(i).get(keys);
            /*     Integer ceil = (int)Math.ceil(Double.valueOf(monthsum.toString()))*10000;//此处数据有差异，后续确认是否向上取整*/
            double ceila = Double.parseDouble(monthsum.toString());
            int ceil = (int) (ceila * 10000);
            roadChart1.add(ceil);
        }
        //去年月总
        ArrayList<Integer> roadChart2 = new ArrayList<>();
        for (int i = 0; i < month; i++) {
            Object monthsum = listdata.get(i).get(snty);
            /*       Integer ceil = (int)Math.ceil(Double.valueOf(monthsum.toString()))*10000;//此处数据有差异，后续确认是否向上取整*/
            double ceila = Double.parseDouble(monthsum.toString());
            int ceil = (int) (ceila * 10000);
            roadChart2.add(ceil);
        }
        cyxt.setDataItem2(roadChart1.toString());
        cyxt.setDataItem3(roadChart2.toString());
        String tbzz10str = JSONObject.toJSONString(cyxt, SerializerFeature.WriteMapNullValue);
        list.add(tbzz10str);
    }




    /**
     * Methad info:计算今年月份截止天数
     * @author 陈兆庆
     * @date 2022/1/5 17:22
     */
   public int getDays(int year,int month) {
       int days=0;
       for (int i = 0; i < month; i++) {
           if (i == 0 || i == 2 || i == 4 || i == 6 || i == 7 || i == 9 || i == 11) {
               days+=31;
           } else if (i == 3 || i == 5 || i == 8 || i == 10) {
               days+=30;
           } else {
               if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
                   days+=29;
               } else {
                   days+=29;
               }
           }
       }
       return days;
   }

    /**
     * Methad info:计算上年月份截止天数
     * @author 陈兆庆
     * @date 2022/1/5 17:22
     */
    public int getlastYearDays(int year,int month) {
        int days=0;
        for (int i = 0; i < month; i++) {
            if (i == 0 || i == 2 || i == 4 || i == 6 || i == 7 || i == 9 || i == 11) {
                days+=31;
            } else if (i == 3 || i == 5 || i == 8 || i == 10) {
                days+=30;
            } else {
                if ((year-1) % 4 == 0 && (year-1) % 100 != 0 || (year-1) % 400 == 0) {
                    days+=29;
                } else {
                    days+=29;
                }
            }
        }
        return days;
    }















    /**
     * Methad info:通用的参数
     * @return com.wonders.cyxtjsonupdate.entity.Cyxt
     * @author 陈兆庆
     * @date 2022/1/4 14:47
     */
    public Cyxt buildgeneralpParame(String id, String mainType, String subType) {
        Cyxt cyxt = new Cyxt();
        cyxt.setId(id);
        cyxt.setMainType(mainType);
        cyxt.setSubType(subType);
        return cyxt;
    }

    /**
     * Methad info:拼接月份
     * @author 陈兆庆
     * @date 2022/1/4 16:14
     */
    public List buildMonth(int month) {
        ArrayList<String> monthList = new ArrayList<>();
        //拼接月份
        for (int i = 0; i < month; i++) {
            if (i == 0) {
                monthList.add("'1月'");
            } else if (i == 1) {
                monthList.add("'2月'");
            } else if (i == 2) {
                monthList.add("'3月'");
            } else if (i == 3) {
                monthList.add("'4月'");
            } else if (i == 4) {
                monthList.add("'5月'");
            } else if (i == 5) {
                monthList.add("'6月'");
            } else if (i == 6) {
                monthList.add("'7月'");
            } else if (i == 7) {
                monthList.add("'8月'");
            } else if (i == 8) {
                monthList.add("'9月'");
            } else if (i == 9) {
                monthList.add("'10月'");
            } else if (i == 10) {
                monthList.add("'11月'");
            } else if (i == 11) {
                monthList.add("'12月'");
            }
        }
        return monthList;
    }
    /**
     * Methad info:
     * @author 陈兆庆
     * @date 2022/1/6 11:16
     */

    public List getMonth2(int month){
        //拼接月份的集合
        ArrayList<String> monthList = new ArrayList<>();
        //拼接月份
        for (int i = 0; i < month; i++) {
            if (i == 0) {
                monthList.add("'一月'");
            } else if (i == 1) {
                monthList.add("'二月'");
            } else if (i == 2) {
                monthList.add("'三月'");
            } else if (i == 3) {
                monthList.add("'四月'");
            } else if (i == 4) {
                monthList.add("'五月'");
            } else if (i == 5) {
                monthList.add("'六月'");
            } else if (i == 6) {
                monthList.add("'七月'");
            } else if (i == 7) {
                monthList.add("'八月'");
            } else if (i == 8) {
                monthList.add("'九月'");
            } else if (i == 9) {
                monthList.add("'十月'");
            } else if (i == 10) {
                monthList.add("'十一月'");
            } else if (i == 11) {
                monthList.add("'十二月'");
            }
        }
        return monthList;
    }
}
